package com.example.utils;

import java.util.Random;

public class RandomChineseUtils {

    // 汉字的 Unicode 范围是 \u4e00 到 \u9fa5
    private static final int CHINESE_START = 0x4e00;
    private static final int CHINESE_END = 0x9fa5;
    private static final Random random = new Random();

    /**
     * 生成单个随机汉字
     *
     * @return 随机汉字
     */
    public static char getRandomChineseCharacter() {
        return (char) (CHINESE_START + random.nextInt(CHINESE_END - CHINESE_START + 1));
    }

    /**
     * 生成指定长度的随机汉字字符串
     *
     * @param length 字符串长度
     * @return 随机汉字字符串
     */
    public static String getRandomChineseString(int length) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(getRandomChineseCharacter());
        }
        return sb.toString();
    }

    /**
     * 生成随机长度的随机汉字字符串
     *
     * @param minLength 最小长度
     * @param maxLength 最大长度
     * @return 随机长度的随机汉字字符串
     */
    public static String getRandomChineseString(int minLength, int maxLength) {
        if (minLength < 0 || maxLength < 0 || minLength > maxLength) {
            throw new IllegalArgumentException("Invalid length range");
        }
        int length = minLength + random.nextInt(maxLength - minLength + 1);
        return getRandomChineseString(length);
    }
}